package blackitheima2;
//购票
public class MyRounnable implements Runnable {
    private int tickCount=100;
    @Override
    public void run() {
        while(true){
            if("窗口一".equals(Thread.currentThread().getName())){
                //同步方法
                boolean result = synchronizedTest();
                if(result){
                    break;
                }
            }

            if("窗口二".equals(Thread.currentThread().getName())){
                synchronized (this){
                    if(tickCount==0){
                        break;
                    }else{
                        try {
                            Thread.sleep(10);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        tickCount--;
                        System.out.println(Thread.currentThread().getName()+"在卖票,剩余"+tickCount+"张票");
                    }
                }
            }


        }
    }

    private synchronized boolean synchronizedTest()  {
        if(tickCount==0){
            return true;
        }else{
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            tickCount--;
            System.out.println(Thread.currentThread().getName()+"在卖票,剩余"+tickCount+"张票");
            return false;
        }
    }
}
